// pages/search/search.js
import dayjs from 'dayjs';

getApp().authPage.basePage({
 
  /**
   * 页面的初始数据
   */
  data: {
    list: null,
    searchListRecord: [],
    searchValue: "",
    searchForm: {
      current: 20,
      size: 1,
      projectShortName: "",
      sortType: 1
    }
  },

  inputRecordValue(e) {
    var {
      value
    } = getApp().utils.getParameter(e);
    if (value) {
      this.setData({
        searchValue: value
      });
      setTimeout(() => {
        this.search()
      }, 200)
    }
  },
  // 时间格式化
  timeFormats(time) {
    return dayjs(time).format('HH:mm');
  },
  // 获取搜索历史
  searchRecord() {
    getApp().http.searchRecord().then(res => {
      console.log("res::::", res);
      if (res.errorCode === 200) {
        this.setData({
          searchListRecord: res.data.map(item => {
            item.createTime = this.timeFormats(item.createTime);
            return item;
          }),
        })
      } else {
        wx.showToast({
          title: '无法获取搜索历史',
          icon: "none"
        })
      }
    })

  },
  setKeyValue(e) {
    this.setData({
      searchValue: e.detail.value
    })
  },
  // 下拉事件
  pullDown() {
    this.setData({
      'searchForm.current': 1,
      'searchForm.size': 20,
    })
    this.getHomeData({
      key: 'down',
    })
  },
  goDetails(e) {
    let id = e.currentTarget.id;
    console.log({
      id
    });
    if (id === undefined) {
      return;
    }
    getApp().nav.navTo({
      url: '/pages/details/details',
      data: {
        id
      },
    })
  },
  onShow() {
    const child = this.selectComponent('#refresher');
    if (child) {
      child.allHideView();
    }
    this.searchRecord();
  },
  search(e) {
    if (this.data.searchValue === "") {
      wx.showToast({
        title: '请输入关键词搜索',
        icon: "none"
      })
      return;
    }
    this.pullDown();
  },
  // 上拉事件 
  pullUp() {
    let current = this.data.searchForm.current;
    current += 1;
    this.setData({
      'searchForm.current': current,
    })
    const child = this.selectComponent('#refresher');
    child.allHideView();
    this.getHomeData({
      key: "up"
    })
  },
  getHomeData({
    key
  }) {
    var child = this.selectComponent('#refresher');
    var that = this;
    this.data.searchForm.projectShortName = this.data.searchValue;
    getApp().http.getHomeData(this.data.searchForm).then(res => {
      if (res.errorCode === 200) {
        if (key === "down") {
          that.setData({
            list: res.data,
          })
        } else if (key === "up") {
          if (that.data.searchForm.current === 1) {
            that.setData({
              list: res.data,
            })
          } else {
            let temp_list = that.data.list;
            let new_list = res.data;
            that.setData({
              list: new_list.concat(temp_list),
            })
          }
        }
        setTimeout(() => {
          if (!child) return;
          child.allHideView();
          if (res.data.length < 10) {
            child.setDataEnd(true);
          }
        }, 1000)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})